/**
 * Project: Utilities <br>
 * Package: org.leumasjaffe.util
 */
package org.leumasjaffe.builder;

import java.lang.reflect.Constructor;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
 * Class org.leumasjaffe.util.CheckedConstructorBuilder in file 
 * CheckedConstructorBuilder.java <br>
 * 
 * @author leumasjaffe
 * @version Jun 7, 2013
 * Created on: Jun 7, 2013
 * 
 * @param <T>
 */
@NonNullByDefault
public class CheckedConstructorBuilder<T> extends ConstructorBuilder<T> implements CheckedBuilder<T> {
	
	private final BuilderChecker checker;
	
	/**
	 * @param constr
	 */
	public CheckedConstructorBuilder(Constructor<? extends T> constr) {
		super(constr);
		this.checker = new BuilderChecker(constr.getParameterTypes().length);
	}
	
	/* (non-Javadoc)
	 * @see org.leumasjaffe.util.ConstructorBuilder#set(int, java.lang.Object)
	 */
	@Override
	public Builder<T> set(int paramNumber, Object arg) {
		Builder<T> self = super.set(paramNumber, arg); 
		// Allows exception to stop setting hasBeenSet[paramNumber] to true
		this.checker.set(paramNumber);
		return self;
	}
	
	/* (non-Javadoc)
	 * @see org.leumasjaffe.util.CheckedBuilder#isFilled()
	 */
	@Override
	public boolean isFilled() {
		return this.checker.isFilled();
	}
	
	@Override
	public T build() {
		validateFilledOrThrow();
		return super.build();
	}

	/**
	 * 
	 */
	private void validateFilledOrThrow() {
		if (!isFilled()) {
			throw new BuilderIncompleteException("Builder is missing parameters");
		}
	}

}
